<?php

namespace app\api\controller\company;
use app\common\service\Result;
use app\shop\model\company\Department as DepartmentModel;
/**
 * 部门管理
 */
class Department
{
    /**
     * 部门列表
     */
    public function lists()
    {
        $app_id = request()->app_id;
        $department_id = input('department_id');
        $map = [
            ['app_id', '=', $app_id],
        ];
        if($department_id){
            $map[] = ['id', '=', $department_id];
        }
        $lists = DepartmentModel::where($map)->with([
            'stations' => function ($query) {
                $query->field('id,title,department_id,trainer')->with([
                    'trainerUser' => function ($q1) {
                        $q1->field('user_id,nickName,mobile,real_name');
                    },
                    'trainFile' => function ($q2) {
                        $q2->field('id,file_name,file_url,station_id,download_num')->order('sort desc');
                    }
                ]);
            }
        ])->field('id,title')->order('pid,sort desc')->select();
        return Result::success($lists);
    }
}